<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Dept;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content;
use App\Models\Anquanyuan;
use App\Models\Dianqi;
use App\Models\Fangjian;
use App\Models\Xhsmhq;
use App\Models\Yinhuan;


class DeptController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Dept(), function (Grid $grid) {

            $grid->quickSearch('deptname')->placeholder('输入部门名称搜索...');
            // 禁用过滤器按钮
            $grid->disableFilterButton();
            // 禁用刷新按钮
            $grid->disableRefreshButton();
            // 禁用行选择器
            $grid->disableRowSelector();

            $grid->export();
            $grid->column('id')->sortable();
            $grid->column('deptname');
            $grid->column('fuzeren');
            // $grid->column('created_at');
            // $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('deptname');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    public function show($id, Content $content)
    {
        return $content->header('部门/科室')
            ->description('部门/科室')
            ->body(Show::make($id, new Dept(), function (Show $show) {
                // $show->field('id');
                $show->field('deptname');
                $show->field('fuzeren');
                // $show->field('created_at');
                // $show->field('updated_at');

                //科室安全员
                $show->relation('安全员', function ($model) {
                    $grid = new Grid(new Anquanyuan);

                    $grid->model()->where('dept_id', $model->id);

                    // 设置路由
                    $grid->setResource('anquanyuans');
                    $grid->column('aqyname', '安全员');
                    $grid->column('aqygonghao');
                    $grid->column('aqytel');


                    $grid->filter(function ($filter) {
                        $filter->like('aqyname')->width('300px');
                    });

                    return $grid;
                });


                //隐患问题
                $show->relation('隐患排查台账', function ($model) {
                    $grid = new Grid(new Yinhuan);


                    $grid->model()->where('dept_id', $model->id);
                    // $grid->model();
                    $grid->model()->orderBy('iswancheng')->orderBy('id', 'desc');

                    // 设置路由
                    $grid->setResource('yinhuans');
                    $grid->column('yinhuanwenti');
                    $grid->column('zerenren');
                    $grid->column('zhenggaicuoshi');
                    $grid->column('zhenggaidate');
                    $grid->column('yinhuanpics')->display(function ($imgs) {
                        $pics = json_decode($imgs);
                        if ($pics) {
                            return $pics[0];
                        }
                    })->image();
                    $grid->column('iswancheng')->switch();
                    $grid->column('zhenggaipics')->display(function ($imgs) {
                        $pics = json_decode($imgs);
                        if ($pics) {
                            return $pics[0];
                        }
                    })->image();
                    // $grid->column('wanchengdate');
                    $grid->column('louzhang');

                    return $grid;
                });


                //科室电器
                $show->relation('非医用电器', function ($model) {
                    $grid = new Grid(new Dianqi);


                    $grid->model()->where('dept_id', $model->id);

                    // 设置路由
                    $grid->setResource('dianqis');
                    $grid->column('dqid');
                    $grid->column('dqname');
                    $grid->column('zrrname');

                    $grid->column('dqgl',);
                    $grid->column('shuliang');
                    $grid->column('weizhi');
                    $grid->column('didian');
                    $grid->column('yongtu');
                    $grid->column('shijian');

                    $grid->filter(function ($filter) {
                        $filter->like('dqname')->width('300px');
                    });

                    return $grid;
                });

                //科室房间台账
                $show->relation('科室房间', function ($model) {
                    $grid = new Grid(new Fangjian);

                    $grid->model()->where('dept_id', $model->id);

                    // 设置路由
                    $grid->setResource('fagnjians');
                    $grid->column('bianhao');
                    $grid->column('fangjianname');
                    $grid->column('weizhi');
                    $grid->column('zrrname');
                    $grid->filter(function ($filter) {
                        $filter->like('dqname')->width('300px');
                    });

                    return $grid;
                });




                //消火栓灭火器防火门
                $show->relation('消火栓灭火器防火门', function ($model) {
                    $grid = new Grid(new Xhsmhq);

                    $grid->model()->where('dept_id', $model->id);

                    // 设置路由
                    $grid->setResource('xhsmhqs');
                    $grid->column('qcname');
                    $grid->column('weizhi');

                    $grid->column('qcbianhao');
                    $grid->column('qczrr');

                    $grid->filter(function ($filter) {
                        $filter->like('qcname')->width('300px');
                    });

                    return $grid;
                });
            }));
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Dept(), function (Form $form) {
            $form->display('id');
            $form->text('deptname');
            $form->text('fuzeren');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
